﻿@model NCP.Model.PhaBaseInfo
@{
    ViewBag.Title = "限制药品使用维护";
}
<div id="LAY-limit-body" class="layui-card" style="margin-bottom:0px;">
    <div class="layui-card-header" style="padding-bottom:10px;padding-top:10px;">
        <span style="margin-left:10px;margin-right:100px;font-size:large;font-weight:600;">@Model.DrugName【@Model.Specs】</span>
        <button id="doSave" class="layui-btn submit">保存</button>
        <button id="onCancel" class="layui-btn layui-btn-danger" type="button">关闭</button>
    </div>
    <div class="layui-row">
        <div class="layui-col-md5">
            <div class="layui-transfer-search" style="border-width: 1px;">
                <i class="layui-icon layui-icon-search"></i>
                <input id="leftsearch" type="text" class="layui-input" placeholder="关键词搜索" />
            </div>
            <table id="left_tab" lay-filter="left"></table>
        </div>
        <div class="layui-col-md1 btn-height">
            <div style="margin-bottom: 10px;">
                <button class="layui-btn  layui-btn-disabled left-btn">
                    <i class="layui-icon">&#xe602;</i>
                </button>
            </div>
            <div>
                <button class="layui-btn layui-btn-disabled right-btn">
                    <i class="layui-icon">&#xe603;</i>
                </button>
            </div>
        </div>
        <div class="layui-col-md6">
            <div class="layui-transfer-search" style="border-width: 1px;">
                <i class="layui-icon layui-icon-search"></i>
                <input id="rightsearch" type="text" class="layui-input" placeholder="关键词搜索" />
            </div>
            <table id="right_tab" lay-filter="right"></table>
        </div>
    </div>
</div>
<script type="text/html" id="StatusTpl">
    <input type="radio" name="Status{{ d.Code }}" value="1" title="限制" {{#  if(d.Status == '1'){ }} checked {{#  } }}>
    <input type="radio" name="Status{{ d.Code }}" value="2" title="放开" {{#  if(d.Status == '2'){ }} checked {{#  } }}>
</script>
<script type="text/javascript" src="~/Scripts/jsonDB.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table', 'admin'], function () {
        var table = layui.table, $ = layui.$, admin = layui.admin;
        //计算按钮的高度
        $('.btn-height').css('padding-top', (parent.document.body.clientHeight / 2 - 150)).css('text-align', 'center');
        //左边表格
        var cart=[];
        admin.req({
            url: '/LimitDrug/LimitPat1' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: "drugCode=@Request["drugCode"]"
            , done: function (res) {
                jsonDB(res.data, 'leftdata').init('leftDB');
                table.render({
                    elem: '#left_tab'
                    , data: res.data
                    , cols: [[
                        { checkbox: true, fixed: true }
                        , { field: 'Code', title: '住院号' }
                        , { field: 'Name', title: '姓名' }
                        , { field: 'DeptName', title: '科室' }
                    ]]
                    , id: 'left_table_id'
                    , height: 'full-120'
                    , page: true
                    , limit: 100
                    , limits: [10, 50, 100, 200, 500, 1000, 2000]
                });
            }
        });
        admin.req({
            url: '/LimitDrug/LimitPat2' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: "drugCode=@Request["drugCode"]"
            , done: function (res) {
                jsonDB(res.data, 'rightdata').init('rightDB');
                table.render({
                    elem: '#right_tab'
                    , data: res.data
                    , cols: [[
                        { checkbox: true, fixed: true }
                        , { field: 'Code', title: '住院号' }
                        , { field: 'Name', title: '姓名' }
                        , { field: 'DeptName', title: '科室' }
                        , { field: 'Status', title: '限制或放开', align: "center", templet: '#StatusTpl', width: 160 }
                    ]]
                    , id: 'right_table_id'
                    , height: 'full-120'
                    , page: true
                    , limit: 100
                    , limits: [10, 50, 100, 200, 500, 1000, 2000]
                });
                //监听单击事件
                table.on('row(right)', function (obj) {
                    obj.tr.find('input[type=radio]:checked').each(function () {
                        obj.data.Status = $(this).val();
                        rightDB.query('update rightdata set Status="' + obj.data.Status + '" where (Code="' + obj.data.Code + '") limit 1');
                    });
                });
            }
        });
        $("#leftsearch").on('input',function () {
            var val = $(this).val().toUpperCase();
            var result = leftDB.findAll("leftdata");
            var ldata = result.filter(e => { return e.Code.indexOf(val) >= 0 || e.Name.indexOf(val) >= 0 || e.DeptName.indexOf(val) >= 0 });
            reloadleftTable(ldata);
        });
        $("#rightsearch").on('input', function () {
            var val = $(this).val().toUpperCase();
            var result = leftDB.findAll("rightdata");
            var rdata = result.filter(e => { return e.Code.indexOf(val) >= 0 || e.Name.indexOf(val) >= 0 || e.DeptName.indexOf(val) >= 0 });
            reloadrightTable(rdata);
        });
        //公共方法
        function getTableData(id) {
            var checkStatus = table.checkStatus(id)
                , data = checkStatus.data;
            return data;
        }
        function btnIf(data, btn) {
            if (data && data.length) {
                $(btn).removeClass('layui-btn-disabled')
            } else {
                $(btn).addClass('layui-btn-disabled')
            }
        }

        function reloadleftTable(ldata) {
            table.reload('left_table_id', {
                data: ldata,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }

        function reloadrightTable(rdata) {
            table.reload('right_table_id', {
                data: rdata,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }
        //监听左表格选中
        table.on('checkbox(left)', function (obj) {
            btnIf(getTableData('left_table_id'), '.left-btn');
        });
        //监听右表格选中
        table.on('checkbox(right)', function (obj) {
            btnIf(getTableData('right_table_id'), '.right-btn');
        });
        //左按钮点击移动数据
        $('.left-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled')
                var data = getTableData('left_table_id');
                for (n in data) {
                    leftDB.query('delete from leftdata where (Code="' + data[n].Code + '") limit 1');
                    rightDB.table('rightdata').addFirst({
                        Code : data[n].Code,
                        Name : data[n].Name,
                        DeptName: data[n].DeptName,
                        Status: data[n].Status
                    });
                }
                reloadleftTable(leftDB.findAll("leftdata"));
                reloadrightTable(rightDB.findAll("rightdata"));
            }
        });
        //右按钮点击移动数据
        $('.right-btn').on('click', function () {
            if (!$(this).hasClass('layui-btn-disabled')) {
                $('.left-btn,.right-btn').addClass('layui-btn-disabled')
                var data = getTableData('right_table_id');
                for (n in data) {
                    var rdata = rightDB.findAll("rightdata").filter(e => { return e.Code == data[n].Code });
                    for (t in rdata) {
                        rightDB.query('delete from rightdata where (Code="' + rdata[t].Code + '") limit 1');
                    }
                    leftDB.table('leftdata').addFirst({
                        Code: data[n].Code,
                        Name: data[n].Name,
                        DeptName: data[n].DeptName,
                        Status: data[n].Status
                    });
                }
                reloadrightTable(rightDB.findAll("rightdata"));
                reloadleftTable(leftDB.findAll("leftdata"));
            }
        });

        $('#doSave').on('click', function () {
            var data = rightDB.findAll("rightdata");
            $.ajax({
                url: '/LimitDrug/SaveLimitPat',
                type: 'post',
                dataType: 'json',
                data: { items: data , drugCode: '@Request["drugCode"]'},
                success: function (res) {
                    layer.alert(res.msg);
                }
            });
        });
        $('#onCancel').on('click', function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
    });
</script>
